package com.videostar.funforfollowers

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSON
import com.likes.followers.insta.more.free.R
import com.videostar.funforfollowers.utils.GooResultBean
import com.videostar.funforfollowers.utils.GooUpdateListener
import com.videostar.funforfollowers.utils.GooglePay
import kotlinx.android.synthetic.main.activity_store.*
import kotlinx.android.synthetic.main.toolbar_coins.*

fun Context.startStore() {
    startActivity(Intent(this, StoreActivity::class.java))
}
class StoreActivity : AppCompatActivity() {

    companion object{
        val iapList by lazy {
            App.sApplication.getIapItems()
        }
        fun getIapItemsIapStrList():MutableList<String> {
            val mutableList = mutableListOf<String>()
            for (i in iapList) {
                mutableList.add(i.iap)
            }
            return mutableList
        }
        class Adapter(val a:(IapBean) -> Unit): RecyclerView.Adapter<Holder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
                return Holder(parent)
            }

            override fun getItemCount(): Int {
                return iapList.size
            }

            override fun onBindViewHolder(holder: Holder, position: Int) {
                holder.show(iapList[position], a)
            }

        }

        class Holder(parent:ViewGroup) : RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.store_rv_item, parent, false)) {
            val coinsNumTv = itemView.findViewById<TextView>(R.id.store_rv_coins)
            val priceTv = itemView.findViewById<TextView>(R.id.store_rv_price)

            fun show(iap:IapBean, a:(IapBean) -> Unit) {
                coinsNumTv.text = iap.coinsNum.toString()
                priceTv.text = "$" + iap.price
                itemView.setOnClickListener {
                    a.invoke(iap)
                }
            }
        }
    }

    var clickNow:IapBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_store)
        back.setOnClickListener {
            onBackPressed()
        }
        GooglePay.init(this, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmpGqBYOPa5y8VPudoj9gzwbGO1GdhoCFP+b3mpSFMxiEJ8rq0Z2B7l5lkQjJDNdl6pT+9oeP7r5mQwYmEZg6XHWdxy6PQTk1xYpVRJ8mitn0u8Vk88ttT0zQ20oIdGwWb1E+fb+++3+LyyrUQqgsUf5usA/rypNnh942niFiEfTBvV2J8xBElP4tgbFlTqUwJGaL2/f9Iny8a0BQitT6+3jZzdzz0vq1D0YrC9Rmu++heE7IEG1p2U5iP1KM8hCMv07nRb45ZEruoHbk6ebl1M79zzF8EetAfsp9EqDmRidt5iKJguMnfnNtFwCJ2x7NmAs3oNjhIgIQaGFeq7pV9wIDAQAB", null, getIapItemsIapStrList(), object : GooUpdateListener {
            override fun onBillingClientSetupFinished() {
                
            }

            override fun onBillingClientSetupError() {
                
            }

            override fun onConsumeFinished(token: String, result: Int) {
                
            }

            override fun onPurchasesUpdated(purchases: MutableList<GooResultBean>) {

                for (i in purchases) {
                    try {
                        val toJSONString = JSON.toJSONString(i)
                        Log.i("pay", toJSONString)
                        if (i.productId == clickNow?.iap) {
                            try {
//                                pay_info.text = toJSONString
                            } catch (e: Exception) {
                            }
                            App.saveCoins(App.getCoins() + (clickNow?.coinsNum?:0))
                            onResume()
                            break
                        }
                    } catch (e: Exception) {
                    }
                }
            }

            override fun errorByUserCanceled(responseCode: Int) {
                
            }

            override fun error(responseCode: Int) {
                
            }

        })

        store_rv.layoutManager = GridLayoutManager(this, 2)
        store_rv.adapter = Adapter {
            try {
                Log.i("pay", JSON.toJSONString(it))
            } catch (e: Exception) {
            }
            clickNow = it
            GooglePay.toBuy(this, it.iap, false)
        }
    }

    override fun onResume() {
        super.onResume()
        coins_number.text = App.getCoins().toString()
    }
}
data class IapBean(val iap:String,val price:String,val coinsNum:Long)

fun Context.getIapItems() :MutableList<IapBean>{
    val list = mutableListOf<IapBean>()
    list.add(IapBean("$packageName.funa", "1.99", 200))
    list.add(IapBean("$packageName.funb", "4.99", 800))
    list.add(IapBean("$packageName.func", "9.99", 2000))
    list.add(IapBean("$packageName.fund", "19.99", 6000))
    list.add(IapBean("$packageName.fune", "49.99", 20000))
    return list
}

